#include<iostream>
#include<pthread.h>
#include<unistd.h>
using namespace std;
pthread_mutex_t lock;
pthread_cond_t cond;
bool flg=true;
void* Routine1(void* args)
{
     while(true)
    {
        pthread_mutex_lock(&lock);
        while(flg!=true)
        {
            pthread_cond_wait(&cond,&lock);//等待
        }
        cout<<(char*)args<<endl;
        flg=false;
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&lock);
    }
    return nullptr;
}

void* Routine2(void* args)
{
    while(true)
    {
        pthread_mutex_lock(&lock);
        while(flg!=false)
        {
            pthread_cond_wait(&cond,&lock);//等待
        }
        cout<<(char*)args<<endl;
        flg=true;
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&lock);
    }
    return nullptr;
}

int main()
{
    pthread_mutex_init(&lock,nullptr);
    pthread_cond_init(&cond,nullptr);

    pthread_t tid_a,tid_b;
    pthread_create(&tid_a,nullptr,Routine1,(void*)"I am thread A...");
    pthread_create(&tid_b,nullptr,Routine2,(void*)"I am thread B...");

    pthread_join(tid_a,nullptr);
    pthread_join(tid_b,nullptr);

    pthread_mutex_destroy(&lock);
    pthread_cond_destroy(&cond);
    return 0;
}